const { UI_Listen, UI_Layer } = require("../../config/ui_def");

cc.Class({
    extends: cc.Component,

    properties: {
        icon: cc.Sprite,
        title_lab: cc.Label,
        skill_lab: cc.Label,
        use_btn: cc.Node,
    },

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {},

    start() {

    },

    // update (dt) {},


    initPage() {

    },

    updatePage(param) {
        if (param.info) {
            this.info = param.info
        }
        this.title_lab.string = `${this.info.name}`
        this.skill_lab.string = `${this.info.description}`
        // this.num_lab.string = ` x${this.info.num}`
        if (this.info.type == 1) {
            this.use_btn.active = false
            this.icon.spriteFrame = he.load.get('item', cc.SpriteAtlas, 'plist').getSpriteFrame(`${this.info.id}`)
        } else if (this.info.type == 9) {
            //图片还未定
            this.icon.spriteFrame = he.load.get('item', cc.SpriteAtlas, 'plist').getSpriteFrame(`${this.info.id}`)
            this.use_btn.active = true
        }
    },

    useBtnServer() {
        // he.local.click('fight')
        gg.eventManager.emit('main', { type: UI_Listen.MAIN_TOMAIN })
        this.closeServer()
        gg.uiManager.addPage(UI_Layer.Cover, {
            name: 'boxPage',
            cover: true,
            boxId: this.changeBoxId()
        })
        let bag = he.local.getJson('bag', 'user')
        if (bag && bag.length > 0) {
            for (let i = 0; i < bag.length; i++) {
                if (bag[i].id == this.info.id) {
                    bag.splice(i, 1)
                    break
                }
            }
        }
        he.local.setJson('bag', bag, 'user')
    },

    changeBoxId() {
        let boxId
        switch (parseInt(this.info.id)) {
            case 91001:
                boxId = 5001
                break
            case 91002:
                boxId = 5002
                break
            case 91003:
                boxId = 5003
                break
            case 91004:
                boxId = 5004
                break
            case 91005:
                boxId = 5005
                break
        }
        return boxId
    },

    closeUI() {

    },

    closeServer() {
        this.uiBase.closePage()
    },
});
